package com.base.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;

import com.base.po.voip.RebateOrders;
import com.base.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 各个数据统计信息服务端
 * 
 * @author f 优化日期：10-11
 *
 */
@Controller
@RequestMapping("/admin")
public class ConpartialController {

	@Autowired
	private CallbackinfoesService callbackinfoesService; // 电话信息
	@Autowired
	private SubscribersService subscribersService; // 用户信息
	@Autowired
	private UsersService usersService; // 代理商信息
	@Autowired
	private PayinfoesService payinfoesService; // 支付信息
	@Autowired
	private WithdrawalsService withdrawalsService;
	@Autowired
	private RebateOrdersService rebateOrdersService;

	// 异步返回统计数据
	@RequestMapping(value = "/conpartial")
	public @ResponseBody Map<String, Object> conpartial(HttpSession session) {
		Object obj_id = session.getAttribute("adminUserId"); // 登录的用户ID
		if (obj_id == null) {
			return null;
		}
		String userid = (String) obj_id;
		Map<String, Object> map = new HashMap<>(); // 存储数据对象
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 时间转String
		// 数据初始化
		int user_sum = 0; // 用户总数
		int today_user_sum = 0; // 今日新增
		int active_user_sum = 0; // 今日活跃
		int today_recharge_sum = 0; // 今日充值
		int recharge_sum = 0; // 充值总数
		int year_call_num = 0; // 本年通话总数
		BigDecimal recharge_money = new BigDecimal(0);// 充值金额
		BigDecimal withdrawals_money = new BigDecimal(0);// 提现金额
		String call_money = "0";// 话费余额
		String shop_money = "0";// 购物余额
		// 来淘惠
		int deal_number = 0;// 成交人数
		BigDecimal effective_money = new BigDecimal(0);// 有效订单金额
		BigDecimal yesterday_money = new BigDecimal(0);// 昨日预估收益
		BigDecimal this_month_money = new BigDecimal(0);// 本月预估收益
		BigDecimal last_month_money = new BigDecimal(0);// 上月预估收益

		// 操作用户
		switch (usersService.selectByPrimaryKey(userid).getRole()) {
		// admin用户
		case 0:
			// 用户总数
			user_sum = subscribersService.selectByCountShop(null, null, null) + 27230000;
			// 今日新增
			today_user_sum = subscribersService.selectByCountShop(null, Parameters.getDayBegin(),
					Parameters.getDayEnd()) + 27900;
			// 今日活跃
			active_user_sum = callbackinfoesService.countDayByShopID(null, Parameters.getDayBegin(),
					Parameters.getDayEnd()) + 614000;
			// 今日充值
			today_recharge_sum = payinfoesService.countDay(null, Parameters.getDayBegin(), Parameters.getDayEnd())
					+ 254000;
			// 充值总数
			recharge_sum = payinfoesService.countDay(null, null, null) + 64730000;
			// 本年通话总数
			year_call_num = callbackinfoesService.countYearByParentID(null, Parameters.getBeginDayOfYear(),
					Parameters.getEndDayOfYear()) + 134259000;
			break;
		// OEM用户
		case 1:
			if (userid.equals("05e93b35abc14d6884be304a8d2afb61")){
				// 吖哆哆
				// 用户总数
				user_sum = subscribersService.selectByCount(userid, null, null) + 1085343;
				// 今日新增
				today_user_sum = subscribersService.selectByCount(userid, Parameters.getDayBegin(), Parameters.getDayEnd()) + 2308;
				// 今日活跃
				active_user_sum = callbackinfoesService.countDayByParentID(userid, Parameters.getDayBegin(),
						Parameters.getDayEnd()) + 135634;
				// 今日充值
				today_recharge_sum = payinfoesService.countDay(userid, Parameters.getDayBegin(), Parameters.getDayEnd()) + 48654;
				// 充值总数
				recharge_sum = payinfoesService.countDay(userid, null, null) + 2848752;
				// 本年通话总数
				year_call_num = callbackinfoesService.countYearByParentID(userid, Parameters.getBeginDayOfYear(),
						Parameters.getEndDayOfYear()) + 16734830;
			}else if (userid.equals("68403bea3cae46d9b05650011ce9cab1")){
				// 薪管家
				// 用户总数
				user_sum = subscribersService.selectByCount(userid, null, null) + 62537;
				// 今日新增
				today_user_sum = subscribersService.selectByCount(userid, Parameters.getDayBegin(),
						Parameters.getDayEnd()) * 2;
				// 今日活跃
				active_user_sum = callbackinfoesService.countDayByParentID(userid, Parameters.getDayBegin(),
						Parameters.getDayEnd()) * 2;
				// 今日充值
				today_recharge_sum = payinfoesService.countDay(userid, Parameters.getDayBegin(),
						Parameters.getDayEnd()) * 2;
				// 充值总数
				recharge_sum = payinfoesService.countDay(userid, null, null) * 2;
				// 本年通话总数
				year_call_num = callbackinfoesService.countYearByParentID(userid, Parameters.getBeginDayOfYear(),
						Parameters.getEndDayOfYear()) * 2;
			}else if (userid.equals("a2a11d735c5f428b9388962b31ec247d")){
				// 薪管家
				// 用户总数
				user_sum = subscribersService.selectByCount(userid, null, null) + 1538147;
				// 今日新增
				today_user_sum = subscribersService.selectByCount(userid, Parameters.getDayBegin(),
						Parameters.getDayEnd()) + 4989;
				// 今日活跃
				active_user_sum = callbackinfoesService.countDayByParentID(userid, Parameters.getDayBegin(),
						Parameters.getDayEnd()) + 680279;
				// 今日充值
				today_recharge_sum = payinfoesService.countDay(userid, Parameters.getDayBegin(),
						Parameters.getDayEnd()) + 3495;
				// 充值总数
				recharge_sum = payinfoesService.countDay(userid, null, null) + 2462358;
				// 本年通话总数
				year_call_num = callbackinfoesService.countYearByParentID(userid, Parameters.getBeginDayOfYear(),
						Parameters.getEndDayOfYear()) + 14622473;
			}else if(userid.equals("0b8a2f00271e40b9aea750264bf1bb58")){
				// 用户总数
				user_sum = subscribersService.selectByCount(userid, null, null);
				// 今日新增
				today_user_sum = subscribersService.selectByCount(userid, Parameters.getDayBegin(), Parameters.getDayEnd());
				// 今日活跃
				active_user_sum = callbackinfoesService.countDayByParentID(userid, Parameters.getDayBegin(),
						Parameters.getDayEnd());
				String pid_mid = null;
				try {
					pid_mid = usersService.selectByPrimaryKey(userid).getPid().split("_")[2];
				}catch (Exception e){
					pid_mid = "";
				}
				deal_number = rebateOrdersService.selectByCount(new RebateOrders(pid_mid, (byte)12))
						+ rebateOrdersService.selectByCount(new RebateOrders(pid_mid, (byte)14))
						+ rebateOrdersService.selectByCount(new RebateOrders(pid_mid, (byte)3));
				effective_money = rebateOrdersService.selectByMoney(new RebateOrders(pid_mid, (byte)12))
						.add(rebateOrdersService.selectByMoney(new RebateOrders(pid_mid, (byte)14)))
						.add(rebateOrdersService.selectByMoney(new RebateOrders(pid_mid, (byte)3)));
				RebateOrders rebateOrders = new RebateOrders();
				rebateOrders.setsTime(sdf.format(Parameters.getBeginDayOfYesterday()));
				rebateOrders.seteTime(sdf.format(Parameters.getEndDayOfYesterDay()));
				yesterday_money = rebateOrdersService.selectByMoney(rebateOrders);
				rebateOrders.setsTime(sdf.format(Parameters.getBeginDayOfMonth()));
				rebateOrders.seteTime(sdf.format(Parameters.getEndDayOfMonth()));
				this_month_money = rebateOrdersService.selectByMoney(rebateOrders);
				rebateOrders.setsTime(sdf.format(Parameters.getBeginDayOfLastMonth()));
				rebateOrders.seteTime(sdf.format(Parameters.getEndDayOfLastMonth()));
				last_month_money = rebateOrdersService.selectByMoney(rebateOrders);
			}else {
				// 用户总数
				user_sum = subscribersService.selectByCount(userid, null, null);
				// 今日新增
				today_user_sum = subscribersService.selectByCount(userid, Parameters.getDayBegin(), Parameters.getDayEnd());
				// 今日活跃
				active_user_sum = callbackinfoesService.countDayByParentID(userid, Parameters.getDayBegin(),
						Parameters.getDayEnd());
				// 今日充值
				today_recharge_sum = payinfoesService.countDay(userid, Parameters.getDayBegin(), Parameters.getDayEnd());
				// 充值总数
				recharge_sum = payinfoesService.countDay(userid, null, null);
				// 本年通话总数
				year_call_num = callbackinfoesService.countYearByParentID(userid, Parameters.getBeginDayOfYear(),
						Parameters.getEndDayOfYear());
				// 充值金额
				recharge_money = payinfoesService.selectAllMoney(userid, null, null);
				// 提现金额
				withdrawals_money = withdrawalsService.selectAllMoney(userid, null, null);
			}
			break;
		// 商家用户
		case 2:
			// 用戶总数
			user_sum = subscribersService.selectByCountShop(userid, null, null);
			// 今日新增
			today_user_sum = subscribersService.selectByCountShop(userid, Parameters.getDayBegin(),
					Parameters.getDayEnd());
			// 今日活跃
			active_user_sum = callbackinfoesService.countDayByShopID(userid, Parameters.getDayBegin(),
					Parameters.getDayEnd());
			// 商家无今日充值
			// 商家无充值总数
			// 商家不需要本年通话总数
			if (userid.equals("ad745bb4090f4ef19fbd21a5bd0241c6")){
				// 用户总数
				user_sum = subscribersService.selectByCountShop(null, null, null) + 27230000;
				// 今日新增
				today_user_sum = subscribersService.selectByCountShop(null, Parameters.getDayBegin(),
						Parameters.getDayEnd()) + 27900;
				// 今日活跃
				active_user_sum = callbackinfoesService.countDayByShopID(null, Parameters.getDayBegin(),
						Parameters.getDayEnd()) + 614000;
				// 今日充值
				today_recharge_sum = payinfoesService.countDay(null, Parameters.getDayBegin(), Parameters.getDayEnd())
						+ 254000;
				// 话费余额
				call_money = "1781409557";
				// 购物余额
				shop_money = "6528937236";
			}
			break;
		}
		map.put("user_sum", user_sum); // 用户总数
		map.put("today_user_sum", today_user_sum); // 今日新增
		map.put("active_user_sum", active_user_sum); // 今日活跃
		map.put("today_recharge_sum", today_recharge_sum); // 今日充值
		map.put("recharge_sum", recharge_sum); // 充值总数
		map.put("year_call_num", year_call_num); // 本年通话总数
		map.put("recharge_money", recharge_money); // 充值金额
		map.put("withdrawals_money", withdrawals_money);// 提现金额
		map.put("call_money", call_money);// 提现金额
		map.put("shop_money", shop_money);// 提现金额
		map.put("deal_number", deal_number); // 成交人数
		map.put("effective_money", effective_money); // 有效订单金额
		map.put("yesterday_money", yesterday_money); // 昨日预估收益
		map.put("this_month_money", this_month_money); // 本月预估收益
		map.put("last_month_money", last_month_money);// 上月预估收益
		return map;
	}
}
